<?php

    /**
     * @package      Modules
     * @subpackage   Admins_Classes_Db
	 *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class DbDisplayPreference extends BaseDbDisplayPreference
    {

    ////////////////////////////////////////////////////////////////////////////
	//                                Methods                                 //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Get preference values for a given administrator
         *
         * @param       DbAdmin     $admin
         * @return      array
         */
        public static function getValues(DbAdmin $admin)
        {   
            $values         = array();
            $preferences    = Doctrine_Query::create()
                                    ->from('DbDisplayPreference dp')
                                    ->where('dp.status = ?', DbBase::ENABLED)
                                    ->execute();

            foreach ($preferences as $preference) {
                $values[$preference->preference_key] = $preference->getValue($admin);
            }

            return $values;
        }

        /**
         * Retrieves preference value
         * 
         * @param       DbAdmin     $admin
         * @return      mixed
         */
        public function getValue(DbAdmin $admin)
        {
            $adminPreference    = Doctrine_Query::create()
                                    ->from('DbAdminPreference ap')
                                    ->where('ap.admin_id = ?', $admin->id)
                                    ->addWhere('ap.preference_id = ?', $this->id)
                                    ->fetchOne();

            $value = ($adminPreference) ? $adminPreference->preference_value : $this->default_value;

            switch ($this->value_type) {
                case 'number':
                    $value = floatval($value);
                    break;
                case 'boolean':
                    $value = ($value == 'true' || $value == 1) ? true : false;
                    break;
            }

            return $value;
        }

        /**
         * Sets preference value
         *
         * @param       DbAdmin     $admin
         * @param       mixed       $value
         * @return      mixed
         */
        public function setValue(DbAdmin $admin, $value)
        {
            $adminPreference    = Doctrine_Query::create()
                                    ->from('DbAdminPreference ap')
                                    ->where('ap.admin_id = ?', $admin->id)
                                    ->addWhere('ap.preference_id = ?', $this->id)
                                    ->fetchOne();

            if ($adminPreference) {
                $adminPreference->preference_value = $value;
                $adminPreference->save();

            } else {
                $adminPreference                    = new DbAdminPreference();
                $adminPreference->status            = DbBase::ENABLED;
                $adminPreference->admin_id          = $admin->id;
                $adminPreference->preference_id     = $this->id;
                $adminPreference->preference_value  = $value;
                $adminPreference->save();
            }
        }

        /**
         * __toString override
         *
         * @return      string
         */
        public function __toString()
        {
            return $this->preference_key;
        }
    }